Telegram Group & Telegram Channel
.🛠 Как логировать SQL-запросы только при ошибках

Часто сталкиваюсь с ситуацией: ошибка 500 в проде, а что именно пошло не так — неясно. Особенно если это была ошибка в SQL-запросе. Логировать все SQL-запросы — слишком жирно и шумно. Но логировать только при исключениях — вот это огонь!

🔍 Решение

Добавьте следующий сниппет в AppServiceProvider:


use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

public function boot()
{
DB::listen(function ($query) {
if (app()->bound('exception.logged')) {
Log::channel('sql')->debug($query->sql, $query->bindings);
}
});

app()->singleton('exception.logged', function () {
return true;
});

app()->error(function (\Throwable $e) {
app()->instance('exception.logged', true);
});
}


🔐 Пояснение
- При каждом SQL-запросе мы проверяем, был ли в этом запросе выброшен exception.
- Если да — логируем SQL.
- Используем отдельный лог-канал sql, чтобы не засорять основной laravel.log.

👉 Создайте в config/logging.php канал sql, направьте его в отдельный файл, например storage/logs/sql.log.


Теперь, если в продакшене будет падение — вы увидите какой SQL-запрос был выполнен перед ошибкой. Это невероятно ускоряет диагностику багов.

👉 @php_lib



tg-me.com/php_lib/731
Create:
Last Update:

.🛠 Как логировать SQL-запросы только при ошибках

Часто сталкиваюсь с ситуацией: ошибка 500 в проде, а что именно пошло не так — неясно. Особенно если это была ошибка в SQL-запросе. Логировать все SQL-запросы — слишком жирно и шумно. Но логировать только при исключениях — вот это огонь!

🔍 Решение

Добавьте следующий сниппет в AppServiceProvider:


use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

public function boot()
{
DB::listen(function ($query) {
if (app()->bound('exception.logged')) {
Log::channel('sql')->debug($query->sql, $query->bindings);
}
});

app()->singleton('exception.logged', function () {
return true;
});

app()->error(function (\Throwable $e) {
app()->instance('exception.logged', true);
});
}


🔐 Пояснение
- При каждом SQL-запросе мы проверяем, был ли в этом запросе выброшен exception.
- Если да — логируем SQL.
- Используем отдельный лог-канал sql, чтобы не засорять основной laravel.log.

👉 Создайте в config/logging.php канал sql, направьте его в отдельный файл, например storage/logs/sql.log.


Теперь, если в продакшене будет падение — вы увидите какой SQL-запрос был выполнен перед ошибкой. Это невероятно ускоряет диагностику багов.

👉 @php_lib

BY Библиотека PHP программиста 👨🏼‍💻👩‍💻


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/php_lib/731

View MORE
Open in Telegram


Библиотека PHP программиста ‍‍ Telegram | DID YOU KNOW?

Date: |

Look for Channels Online

You guessed it – the internet is your friend. A good place to start looking for Telegram channels is Reddit. This is one of the biggest sites on the internet, with millions of communities, including those from Telegram.Then, you can search one of the many dedicated websites for Telegram channel searching. One of them is telegram-group.com. This website has many categories and a really simple user interface. Another great site is telegram channels.me. It has even more channels than the previous one, and an even better user experience.These are just some of the many available websites. You can look them up online if you’re not satisfied with these two. All of these sites list only public channels. If you want to join a private channel, you’ll have to ask one of its members to invite you.

How to Use Bitcoin?

n the U.S. people generally use Bitcoin as an alternative investment, helping diversify a portfolio apart from stocks and bonds. You can also use Bitcoin to make purchases, but the number of vendors that accept the cryptocurrency is still limited. Big companies that accept Bitcoin include Overstock, AT&T and Twitch. You may also find that some small local retailers or certain websites take Bitcoin, but you’ll have to do some digging. That said, PayPal has announced that it will enable cryptocurrency as a funding source for purchases this year, financing purchases by automatically converting crypto holdings to fiat currency for users. “They have 346 million users and they’re connected to 26 million merchants,” says Spencer Montgomery, founder of Uinta Crypto Consulting. “It’s huge.”

Библиотека PHP программиста ‍‍ from sg


Telegram Библиотека PHP программиста 👨🏼‍💻👩‍💻
FROM USA